// 主函数所在位置
#include "paral_py.h"
#include "input_paral_fun_MR.h"
#include <unistd.h>

int main(int argc, char *argv[]){

    std::vector<std::string> seq_functions;
    std::string paral_function = "";
    std::string input_file_name = "";
    std::string record_file_name = "";
    std::string target_code_type = "local";

    int opt;

    // 用-r参数来输出报告
    // 用-t参数来指定要生成的代码
    
    const char *optstring = "i:p:s:r:t:";

    
    while ((opt = getopt(argc, argv, optstring)) != -1) {
        if(opt == 'r'){
            record_file_name = optarg;
            continue;
        }

        if(opt == 'i'){
            input_file_name = optarg;
            continue;
        }
        
        if(opt == 'p'){
            paral_function = optarg;
            continue;
        }

        if(opt == 's'){
            seq_functions.push_back(optarg);    
            continue;
        }

        if(opt == 't'){
            target_code_type = optarg;
            continue;
        }

        // 这里说明出现不合规参数
        printf("error param\n");
        exit(-1);
    }

    assert(paral_function != "");
    assert(input_file_name != "");


    struct paral_py_context ctx;
    init_paral_py_context(&ctx, input_file_name.c_str());

    // seq_functions.push_back("sub");

    // seq_functions.push_back("add");

    // seq_functions.push_back("equal_to");

    // seq_functions.push_back("value_of");

    // seq_functions.push_back("key_of");
    if(record_file_name != ""){
        // 对文件进行输出
        std::ofstream out;
        out.open(record_file_name);
        out << "{" << std::endl;
        out << build_pre_analysis_report(&ctx, seq_functions, paral_function) << std::endl;
        out << "}" << std::endl;
    }else{
        // 如果不是预处理，就根据情况输出代码
        if(target_code_type == "local"){
            printf("local:\n");
            build_local_seq_file(&ctx, seq_functions, paral_function);
        }else if(target_code_type == "hadoop_MR"){
            printf("hadoop_MR:\n");
            build_mapper_reducer_file(&ctx, seq_functions, paral_function);
        }
    }

    
}
